package entity;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.StringTokenizer;

import org.newdawn.slick.Image;
import org.newdawn.slick.SlickException;

public class Map {
	
	public static final String INITIAL_MAP_IMG = "data/maps/Routex5y5.png";
	public static final String INITIAL_MAP_TXT = "data/maps/Routex5y5.txt";
	
	private Image image; // Imagem do mapa.
	private int[][] squaresValue; // Matriz contendo o valor de cada quadrado do mapa.

	
	public Map() throws SlickException{
		this(new Image(INITIAL_MAP_IMG), INITIAL_MAP_TXT);
	}
	
	public Map(Image image, String arquivo){
			setImage(image);
			setSquareValues(arquivo);			
	}
	
	public void setSquareValues(String nome){
		System.out.println("Abrindo arquivo " + nome);
		squaresValue = new int[20][20];
		File mapaTxt = new File(nome);
		FileReader reader = null;
		try {
			reader = new FileReader(mapaTxt);
		} catch (FileNotFoundException e) {
			System.out.println("Impossivel ler o arquivo.");
		}
		BufferedReader rd = new BufferedReader(reader);
		StringTokenizer st;
		String line = null;

		for(int i = 0; i < 20; i++){
			try {
				line = rd.readLine();
			} catch (IOException e) {
				System.out.println("Impossivel ler a linha do mapa.");
			}
			st = new StringTokenizer(line, ",");	
			for (int j = 0; j < 20; j++) {
				squaresValue[j][i] = Integer.parseInt(st.nextToken());				
			}
		}
	
	}

	public Image getImage() {
		return image;
	}

	public void setImage(Image image) {
		this.image = image;
	}

	public int getSquareValue(int x, int y) {
		return squaresValue[x][y];
	}
}